<cfcomponent extends="Controller" output="false">
	
	<cffunction name="index">
		<cfset seasons = model("Season").findAll()>
	</cffunction>
	
	<!--- -seasons/show/key --->
	<cffunction name="show">
		
		<!--- Find the record --->
    		<cfset season = model("Season").findByKey(key=params.key,include="Fixture")>
		
    	
    		<!--- Check if the record exists --->
	    	<cfif NOT IsObject(season)>
			<cfset flashInsert(error="Season #params.key# was not found")>
			<cfset redirectTo(action="index")>
	    	</cfif>	    
		
		<!--- Show Teams for this season --->
		<cfset teams = model("Team").findAll(where="SeasonID='#params.key#'")>
		<cfset listTeamIDs = ValueList(teams.teamid)>
		
		<!--- Lock teams if a fixture has been made --->
		<cfif Len(season.FixtureCreated)>
			<cfset editableTeam = false>
		</cfif>
		
		<!--- Add Team dropdown --->
		<cfset avaliableTeams = model("Team").findAll(where="SeasonID!='#params.key#'")>
		
		<!--- Find any fixtures for this season --->
		<cfset fixtures = model("Fixture").findAll(where="SeasonID='#params.key#'",include="Result",order="RoundNo")>
		
		<cfset recentResults = model("Result").findAll(where="SeasonID='#season.seasonID#'",include="Fixture")>
		<!---<cfset ladder = model("Result").getLadder(season.seasonID)>--->
		<cfset ladder = model("Ladder").findAll(where="SeasonID='#season.seasonID#'",select="TeamID,Gamesfor,GamesAgainst,Points",order="Points desc,GamesFor desc")>
		
	</cffunction>
		
	<!--- -seasons/new --->
	<cffunction name="new">
		
		<cfset season = model("Season").new()>
		
		<cfset weeks = generateWeeks()>

	</cffunction>
	
	<cffunction name="generateWeeks">
		<cfset var result = ArrayNew(1)>
		<cfloop from="#Week(CreateDate(year(now()),1,1))#" to="#Week(CreateDate(year(now()),12,31))#" index="i">
			<cfset ArrayAppend(result,i)>
		</cfloop>
		<cfreturn result>
	</cffunction>
	
	<!--- Step 2, setup the teams --->
	<cffunction name="seasonTeams">
		<cfset season = model("Season").findByKey(params.key)>
	</cffunction>
	
	<!--- -seasons/edit/key --->
	<cffunction name="edit">
	
		<!--- Find the record --->
    	<cfset season = model("Season").findByKey(params.key)>
    	
    	<!--- Check if the record exists --->
	    <cfif NOT IsObject(season)>
	        <cfset flashInsert(error="Season #params.key# was not found")>
			<cfset redirectTo(action="index")>
	    </cfif>
		
	</cffunction>
	
	<!--- -seasons/create --->
	<cffunction name="create">
		<cfif isPost() AND StructKeyExists(params,"season")>
			<cfset season = model("Season").new(params.season)>
			<!--- Verify that the season creates successfully --->
			<cfif season.save()>
				<cfset flashInsert(success="The season was created successfully. #season.seasonName#")>
				<cfif isDefined("params.redirect")>
					<cfset redirectTo(action=params.redirect,key=season.seasonid)>
				</cfif>
	            <cfset redirectTo(action="index")>
			<!--- Otherwise --->
			<cfelse>
				<cfset renderPage(action="new")>
			</cfif>
		<cfelse>
			<cfset render404()>
		</cfif>
	</cffunction>
	
	<!--- -seasons/update --->
	<cffunction name="update">
		<cfset season = model("Season").findByKey(params.key)>
		
		<!--- Verify that the season updates successfully --->
		<cfif season.update(params.season)>
			<cfset flashInsert(success="The season was updated successfully.")>	
            <cfset redirectTo(action="index")>
		<!--- Otherwise --->
		<cfelse>
			<cfset renderPage(action="edit")>
		</cfif>
	</cffunction>
	
	<!--- -seasons/delete/key --->
	<cffunction name="delete">
		<cfset season = model("Season").findByKey(params.key)>
		
		<!--- Verify that the season deletes successfully --->
		<cfif season.delete()>
			<cfset flashInsert(success="'#season.seasonName#' was deleted successfully.")>	
            <cfset redirectTo(action="index")>
		<!--- Otherwise --->
		<cfelse>
			<cfset flashInsert(error="Error occurred;" & errorMessagesFor("season"))>
			<cfset redirectTo(action="index")>
		</cfif>
	</cffunction>
	
</cfcomponent>
